import allure
import requests

from conf.env import URL





class BaseRequest:
    # 定义类变量
    sess = requests.session()
    @classmethod
    def send_request(cls,**kwargs):
        return cls.sess.request(**kwargs)
    @classmethod
    def execute_api_request(cls, request={"epic": "接口自动化项目",
                                          "feature": "用户相关",
                                          "story": "登录金融项目登录接口",
                                          "method": "post",
                                          "url": "/pinter/bank/api/login",
                                          "headers": None,
                                          "cookies": None},
                            parametrize={"title": "输入正确用户名和正确密码,登录成功",
                                         "data": {"userName": "admin", "password": "123456"},
                                         "expect": {"status_code": '0', "msg": "success"},
                                         "params": None,
                                         "json": None,
                                         "files": None}):
        url = URL + request["url"]
        allure.attach(url, "url地址:", attachment_type=allure.attachment_type.TEXT)
        method = request["method"]
        allure.attach(method, "请求方法:", attachment_type=allure.attachment_type.TEXT)
        headers = request["headers"]
        if headers is not None:
            allure.attach(headers, "headers:", attachment_type=allure.attachment_type.TEXT)
        cookies =request["cookies"]
        if cookies is not None:
            allure.attach(cookies, "cookies:", attachment_type=allure.attachment_type.TEXT)
        data = parametrize["data"]
        if data is not None:
            allure.attach(str(data), "data:", attachment_type=allure.attachment_type.TEXT)
        params = parametrize["params"]
        if params is not None:
            allure.attach(str(params), "params:", attachment_type=allure.attachment_type.TEXT)
        json = parametrize["json"]
        if json is not None:
            allure.attach(str(json), "json:", attachment_type=allure.attachment_type.TEXT)
        files = parametrize["files"]
        if files is not None:
            allure.attach(str(files), "files:", attachment_type=allure.attachment_type.TEXT)
        rp = cls.send_request(method=method,url=url,headers=headers,
                              data=data,params=params,json=json,files=files,
                              cookies=cookies,
                              timeout=10,verify=False)
        expect = parametrize["expect"]
        allure.attach(str(expect), "预期结果:", attachment_type=allure.attachment_type.TEXT)
        
        allure.attach(rp.text, "实际结果:", attachment_type=allure.attachment_type.TEXT)
        return rp
    
if __name__ == '__main__':
        print(BaseRequest.execute_api_request().text)
    